﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerGame.Service.Dispatcher;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using System;
using ServerBase.Other;
using System.Text;
using ServerBase.Config;
using ServerGame.Manager;
using ServerGame.GameObject;
using ServerPublic.Manager;

namespace ServerGame.Manager
{
    /// <summary>
    /// 奖励
    /// </summary>
    public static class AwardMgr
    {
        // 发送奖励
        public static void AwardSend(this Player player, List<CLS_AwardItem> ListAward, EActionSource source)
        {
            loger.Debug($"发送奖励 SendAwardItems {ListAward.Count}");
            if (ListAward.Count <= 0)
            {
                return;
            }
            List<CLS_AwardItem> ListRemove = new List<CLS_AwardItem>();
            foreach (var item in ListAward)
            {
                var AwardType = (EAwardType)item.Type;
                switch (AwardType)
                {
                    //case EAwardType.道具:
                    //    player.ItemAdd(item.ConfigId, item.Count, source);
                    //    break;
                    //case EAwardType.英雄:
                    //    player.HeroAdd(item.ConfigId, item.Count, source);
                    //    break;
                    default:
                        player.ValueAdd(AwardType, item.Count, source);
                        break;
                }
            }
            ListAward.RemoveAll(it => (ListRemove.Contains(it)));
        }

        #region 扩展
        //添加奖励项
        public static void AddAwardCount(this List<CLS_AwardItem> ListAward, EAwardType awardType, long Count)
        {
            ListAward.AddAward(awardType, 0, Count, 0);
        }
        public static void AddAwardConfigId(this List<CLS_AwardItem> ListAward, EAwardType awardType, int ConfigId, long Count = 1)
        {
            ListAward.AddAward(awardType, ConfigId, Count, 1);
        }
        public static void AddAward(this List<CLS_AwardItem> ListAward, EAwardType awardType, int ConfigId, long Count, int Level)
        {
            ListAward.Add(new CLS_AwardItem() { Type = (short)awardType, ConfigId = ConfigId, Count = Count, Level = Level, });
        }
        //合并奖励相同项
        public static List<CLS_AwardItem> Combine(this List<CLS_AwardItem> ListAward)
        {
            var results = new List<CLS_AwardItem>();
            foreach (var item in ListAward)
            {
                bool combine = false;
                foreach (var result in results)
                {
                    if (item.Type == result.Type

                        && item.ConfigId == result.ConfigId
                        && item.Level == result.Level
)
                    {
                        result.Count += item.Count;
                        combine = true;
                        break;
                    }
                }
                if (!combine)
                {
                    results.Add(item.Clone());
                }
            }
            return results;
        }
        //乘倍数简单
        public static void MultiAwardItemsSimple(this List<CLS_AwardItem> ListAward, int amount)
        {
            foreach (var item in ListAward)
            {
                item.Count *= amount;
            }
        }
        //乘倍数
        public static List<CLS_AwardItem> MultiAwardItems(this List<CLS_AwardItem> ListAward, int amount)
        {
            var results = new List<CLS_AwardItem>();
            foreach (var item in ListAward)
            {
                var copy = item.Clone();
                copy.Count = copy.Count * amount;
                results.Add(copy);
            }
            return results;
        }
        //打印
        public static string Dump(this CLS_AwardItem Award)
        {
            return $"Type:{Award.Type} Type:{(EAwardType)Award.Type} ConfigId:{Award.ConfigId} Count:{Award.Count} Level:{Award.Level}";
        }

        #region 固定



        #endregion

        #region 掉落组
        public class SettDigWeightItem
        {
            public int Id;
            public int ScaleMin;
            public int ScaleMax;
            public SettDigWeightItem(List<int> v, int v1, int v2)
            {
                Id = v[0];
                ScaleMin = v1;
                ScaleMax = v2;
            }
        }
        //根据掉落组获取掉落
        public static void ToDropGroupListAward(this List<List<int>> ListAwardText, ref List<CLS_AwardItem> ListAward, EActionSource source)
        {
            foreach (var AwardText in ListAwardText)
            {
                if (AwardText.Count >= 2)
                {
                    if (Util.RandLessThen(1_0000, AwardText[1]))
                    {
                        GetListAwardDrop(AwardText[0], ref ListAward, source);
                    }
                }
            }
        }
        #endregion


        #region 掉落表

        //生成奖励项列表
        public static bool GetListAwardDrop(int DropId, ref List<CLS_AwardItem> ListAward, EActionSource source)
        {
            //if (DropId == 0)
            //{
            //    return false;
            //}
            if (!ConfDropExt.TryGetValue(DropId, out var conf))
            {
                loger.Warn($"GetListAwardDrop {source} 找不到掉落ID DropId:{DropId}");
                return false;
            }
            switch ((EDropType)conf.DropType)
            {
                case EDropType.固定掉落:
                    if (true)
                    {
                        conf.DropPoll.ToListAward(ref ListAward);
                    }
                    break;
                case EDropType.独立掉落:
                    if (true)
                    {
                        foreach (var kvp in conf.DictDropPoll)
                        {
                            if (Util.RandLessThen(100_0000, kvp.Value))
                            {
                                ListAward.Add(kvp.Key);
                            }
                        }
                    }
                    break;
                case EDropType.抽取放回:
                    if (true)
                    {
                        int amount = 0;
                        //先获取个数
                        if (conf.Num.Count > 0)
                        {
                            amount = conf.RateNum.SelectSingle();
                        }
                        //抽取
                        if (amount > 0 && conf.DictDropPoll.Count > 0)
                        {
                            var ResultPool = conf.RateDropPoll.SelectMultiple(amount, true);
                            ListAward.AddRange(ResultPool);
                        }
                    }
                    break;
                case EDropType.抽取不放回:
                    if (true)
                    {
                        int amount = 0;
                        //先获取个数
                        if (conf.Num.Count > 0)
                        {
                            amount = conf.RateNum.SelectSingle();
                        }
                        //抽取
                        if (amount > 0 && conf.DictDropPoll.Count > 0)
                        {
                            var ResultPool = conf.RateDropPoll.SelectMultiple(amount, false);
                            ListAward.AddRange(ResultPool);
                        }
                    }
                    break;
                default:
                    loger.Warn($"GetListAwardDrop {source} 掉落类型错误 DropId:{DropId} DropType:{conf.DropType}");
                    break;
            }
            bool Warn = false;
            foreach (var item in ListAward)
            {
                if (item.Count > 9999999)
                {
                    Warn = true;
                    break;
                }
            }
            if (Warn)
            {
                loger.Warn($"GetListAwardDrop {source} 掉落ID数量异常 DropId:{DropId}");
            }
            return true;
        }
        #endregion
        #endregion


    }
}